﻿package mortal.game.view.daily.view
{
    import Message.DB.Tables.*;
    import com.gengine.resource.*;
    import com.mui.containers.globalVariable.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import fl.data.*;
    import flash.events.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.guide.*;
    import mortal.game.view.daily.*;
    import mortal.game.view.daily.render.*;
    import mortal.mvc.core.*;

    public class DailyVitalityShowView extends GSprite
    {
        private var _bg:GBitmap;
        private var _num:BitmapNumberText;
        private var _vitNum:BitmapNumberText;
        private var _bar:BaseProgressBar;
        private var _rewardBtn:GLoadingButton;
        private var _list:GTileList;
        private static var _instance:DailyVitalityShowView;

        public function DailyVitalityShowView()
        {
            this.isHideDispose = true;
            return;
        }// end function

        private function setData() : void
        {
            var _loc_1:* = Cache.instance.daily;
            var _loc_2:* = _loc_1.data.activity > DailyUtil.maxActivity ? (DailyUtil.maxActivity) : (_loc_1.data.activity);
            this._vitNum.text = "" + _loc_2;
            this._bar.setValue(_loc_2, DailyUtil.maxActivity);
            this._list.dataProvider = new DataProvider(DailyConfig.instance.allActivityData);
            this._list.drawNow();
            this.checkReward();
            var _loc_3:* = DailyConfig.instance.getActivityItem((_loc_1.data.level + 1));
            this._rewardBtn.filterEnabled = _loc_3 && _loc_1.data.activity >= _loc_3.activity;
            return;
        }// end function

        private function checkReward() : void
        {
            var _loc_3:DailyVitalityShowViewRenderer = null;
            var _loc_1:Boolean = false;
            var _loc_2:int = 0;
            while (_loc_2 < this._list.dataProvider.length)
            {
                
                _loc_3 = this._list.itemToCellRenderer(this._list.getItemAt(_loc_2)) as DailyVitalityShowViewRenderer;
                if (_loc_3 && _loc_3.hasReward)
                {
                    _loc_1 = true;
                    break;
                }
                _loc_2++;
            }
            if (_loc_1)
            {
                EffectManager.glowFilterReg(this._rewardBtn);
            }
            else
            {
                EffectManager.glowFilterUnReg(this._rewardBtn);
            }
            return;
        }// end function

        private function onClickRewardBtn(event:MouseEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.DailyGetReward));
            return;
        }// end function

        private function onRewardGetSucceed(param1:Object) : void
        {
            this._rewardBtn.filterEnabled = false;
            AutoGuideRedPoint.instance.setValueByName(AutoGuideRedPoint.DailyVitalityAdd, false);
            Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideRedPointRefresh));
            return;
        }// end function

        private function onResCompl() : void
        {
            pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.DailyVitalityBg2, 0, 0, this));
            pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.DailyTxt2, 15, 98, this));
            pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.DailyTxt4, -405, 3, this));
            this.addChild(this._bar);
            this.addChild(this._vitNum);
            this.addChild(this._rewardBtn);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildren();
            pushUIToDisposeVec(UIFactory.bg(-425, 0, 425, 26, this, ImagesConst.RegionTitleBg));
            this._bg = UIFactory.gBitmap("", 0, 0, this);
            this._num = UIFactory.bitmapNumberText(-298, 4, "DailySlantNum.png", 19, 20, -9, this);
            this._vitNum = UIFactory.bitmapNumberText(132, 93, "DailyYellow.png", 36, 41, 2);
            this._bar = UICompomentPool.getUICompoment(BaseProgressBar);
            this._bar.setBg(ImagesConst.VipProgressBg, true, 355, 30);
            this._bar.setProgress(ImagesConst.VipProgress, true, 10, 10, 335, 14);
            this._bar.setLabel(BaseProgressBar.ProgressBarTextNumber, 125, 5, 75, 8);
            UIFactory.setObjAttri(this._bar, 18, 30, -1, -1, this);
            this._rewardBtn = UIFactory.gLoadingButton(ResFileConst.DailyRewardBtn, 255, 90, 103, 40);
            this._list = UIFactory.tileList(7, 151, 410, 400, this);
            this._list.setStyle("cellRenderer", DailyVitalityShowViewRenderer);
            this._list.columnWidth = 192;
            this._list.rowHeight = 74;
            this._list.direction = GBoxDirection.HORIZONTAL;
            this._rewardBtn.configEventListener(MouseEvent.CLICK, this.onClickRewardBtn);
            NetDispatcher.addCmdListener(ServerCommand.DailyRewardGetSucceed, this.onRewardGetSucceed);
            LoaderHelp.addResCallBack(ResFileConst.daily, this.onResCompl);
            return;
        }// end function

        public function set num(param1:int) : void
        {
            this._num.text = param1.toString();
            this.setData();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            LoaderHelp.removeResEvent(ResFileConst.daily, this.onResCompl);
            NetDispatcher.removeCmdListener(ServerCommand.DailyRewardGetSucceed, this.onRewardGetSucceed);
            this._bg.dispose(param1);
            this._num.dispose(param1);
            this._bar.dispose(param1);
            this._vitNum.dispose(param1);
            this._rewardBtn.dispose(param1);
            this._list.dispose(param1);
            this._bg = null;
            this._num = null;
            this._bar = null;
            this._vitNum = null;
            this._rewardBtn = null;
            this._list = null;
            return;
        }// end function

        public static function get instance() : DailyVitalityShowView
        {
            if (!_instance)
            {
                _instance = new DailyVitalityShowView;
            }
            return _instance;
        }// end function

        public static function get hasInstance() : Boolean
        {
            return _instance != null;
        }// end function

    }
}
